using Tycho.Parser.Tokens;

namespace Tycho.Parser.Peg {
    public class NamedRule<Context> : Rule<Context> {
        public string Name;
        public Production<Context> Production;

        public NamedRule (string name, Production<Context> production) {
            Name = name;
            Production = production;
        }

        protected override RuleParseResult<Context> ReallyParse (Token[] tokens, int index, Context context) {
            ParseResult<Context> result = Production.Parse (tokens, index, context);
            if (result != null) {
                var ruleResult = new RuleParseResult<Context> (result.Index, result.Context);
                ruleResult.AddCapture (Name, result.Term);
                return ruleResult;
            } else {
                return null;
            }
        }
    }
}